package presenters.component;

public class PageComponent {

    def static namespace = "gp"

    def anchor = {args, callback ->
        args.href = gp.createLink(args)
        builder.a(args, callback())
    }

    def createLink = {args ->
        def href = new StringBuilder()
        href << "/groovypublish/${args.remove('controller')}/${args.remove('action')}"
        if (args.id) {
            href << "/${args.remove('id')}"
        }
        if (args.params) {
            append(href, args.params)
        }
        href
    }

    def textInput = {args ->
        builder.dt(args.label)
        builder.dd {
            input(type: "text", name: args.property, value: args.bean[args.property], size: args.size)
        }
    }

    def textArea = {args ->
        def value = (args.bean[args.property])?:"" 
        builder.dt(args.label)
        builder.dd {
            textarea(name: args.property, rows: args.rows, cols: args.cols, value){}
        }
    }

    def append(href, params) {
        def paramMap = new GroovyShell().evaluate(params)
        def paramList = []
        paramMap.each {key, value ->
            paramList << "${key}=${value}"
        }
        href << "?"
        href << paramList.join("&")
    }
}
